package com.gzdh.dishrecognition.ui.fragment.sysset;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.SysConstConfig;
import com.gzdh.dishrecognition.emu.CardSectorPassEmus;
import com.gzdh.dishrecognition.emu.OfflineGetCardSnTypeEmus;
import com.gzdh.dishrecognition.emu.PeripheralDeviceEmus;
import com.gzdh.dishrecognition.emu.ReadCardTypeEmus;
import com.gzdh.dishrecognition.ui.dialog.InputDialog;
import com.gzdh.dishrecognition.ui.dialog.MsgDialog;
import com.gzdh.dishrecognition.ui.view.OptionItemView;

import java.util.LinkedHashMap;
import java.util.Map;

public class SetPayFragment extends Fragment {
    private Context mContext;

    private Activity activity;


    private OptionItemView successCloseBut;

    private OptionItemView errorCloseBut;


    private OptionItemView successCloseSecBut;

    private OptionItemView errorCloseSecBut;


    private OptionItemView weixinPayBut;

    private OptionItemView aliPayBut;


    private OptionItemView cardPayBut;

    private OptionItemView facePayBut;

    private OptionItemView cashPayBut;

    private OptionItemView readCardTypeBut;

    private OptionItemView offlineGetCardSnTypeBut;

    private OptionItemView cardDataSectorBut;

    private OptionItemView cardSectorPassBut;


    public SetPayFragment(Activity activity) {
        this.activity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_pay_set, container, false);
        mContext = view.getContext();
        successCloseSecBut = view.findViewById(R.id.successCloseSecBut);
        errorCloseSecBut = view.findViewById(R.id.errorCloseSecBut);
        successCloseBut = view.findViewById(R.id.successCloseBut);
        errorCloseBut = view.findViewById(R.id.errorCloseBut);
        weixinPayBut = view.findViewById(R.id.weixinPayBut);
        aliPayBut = view.findViewById(R.id.aliPayBut);
        cardPayBut = view.findViewById(R.id.cardPayBut);
        facePayBut = view.findViewById(R.id.facePayBut);
        cashPayBut = view.findViewById(R.id.cashPayBut);
        readCardTypeBut = view.findViewById(R.id.readCardTypeBut);
        offlineGetCardSnTypeBut = view.findViewById(R.id.offlineGetCardSnType);

        cardDataSectorBut = view.findViewById(R.id.cardDataSectorBut);

        cardSectorPassBut = view.findViewById(R.id.cardSectorPassBut);
        weixinPayBut.setOnClickListener(view1 -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("启动微信付款码支付");
            final Map<String, Boolean> map = new LinkedHashMap<>();
            map.put("开启", true);
            map.put("关闭", false);
            final String[] keysTemp = new String[2];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    boolean state = map.get(keys[which]);
                    SysConstConfig.getInstance().setAllowWeixin(state);
                    showState();
                }
            });
            builder.show();
        });


        aliPayBut.setOnClickListener(view1 -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("启动支付宝付款码支付");
            final Map<String, Boolean> map = new LinkedHashMap<>();
            map.put("开启", true);
            map.put("关闭", false);
            final String[] keysTemp = new String[2];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    boolean state = map.get(keys[which]);
                    SysConstConfig.getInstance().setAllowAli(state);
                    showState();
                }
            });
            builder.show();
        });


        cardPayBut.setOnClickListener(view1 -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("启动IC支付");
            final Map<String, Boolean> map = new LinkedHashMap<>();
            map.put("开启", true);
            map.put("关闭", false);
            final String[] keysTemp = new String[2];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    boolean state = map.get(keys[which]);
                    SysConstConfig.getInstance().setAllowCard(state);
                    showState();
                }
            });
            builder.show();
        });


        cashPayBut.setOnClickListener(view1 -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("启动现金支付");
            final Map<String, Boolean> map = new LinkedHashMap<>();
            map.put("开启", true);
            map.put("关闭", false);
            final String[] keysTemp = new String[2];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    boolean state = map.get(keys[which]);
                    SysConstConfig.getInstance().setAllowCash(state);
                    showState();
                }
            });
            builder.show();
        });


        facePayBut.setOnClickListener(view1 -> {
            Toast.makeText(mContext, "该功能暂未开放", Toast.LENGTH_LONG).show();
        });

        successCloseBut.setOnClickListener(view1 -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("支付成功自动关闭");
            final Map<String, Boolean> map = new LinkedHashMap<>();
            map.put("开启", true);
            map.put("关闭", false);
            final String[] keysTemp = new String[2];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    boolean state = map.get(keys[which]);
                    SysConstConfig.getInstance().setSuccessClose(state);
                    showState();
                }
            });
            builder.show();
        });
        errorCloseBut.setOnClickListener(view1 -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("支付失败自动关闭");
            final Map<String, Boolean> map = new LinkedHashMap<>();
            map.put("开启", true);
            map.put("关闭", false);
            final String[] keysTemp = new String[2];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    boolean state = map.get(keys[which]);
                    SysConstConfig.getInstance().setErrorClose(state);
                    showState();
                }
            });
            builder.show();
        });


        successCloseSecBut.setOnClickListener(view1 -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            EditText text = new EditText(activity);
            text.setInputType(InputType.TYPE_CLASS_NUMBER);
            builder.setTitle("请输入支付成功自动关闭等待时间（秒）").setIcon(android.R.drawable.ic_dialog_info).setView(text).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String sec = text.getText().toString();
                    if (sec == null || "".equals(sec)) {
                        Toast.makeText(mContext, "您的输入有误，请检查", Toast.LENGTH_LONG).show();
                        return;
                    }
                    int count = 0;
                    try {
                        count = Integer.parseInt(sec);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    SysConstConfig.getInstance().setSuccessCloseSec(count);
                    showState();
                }
            }).show();
        });


        errorCloseSecBut.setOnClickListener(view1 -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            EditText text = new EditText(activity);
            text.setInputType(InputType.TYPE_CLASS_NUMBER);
            builder.setTitle("请输入支付失败自动关闭等待时间（秒）").setIcon(android.R.drawable.ic_dialog_info).setView(text).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String sec = text.getText().toString();
                    if (sec == null || "".equals(sec)) {
                        Toast.makeText(mContext, "您的输入有误，请检查", Toast.LENGTH_LONG).show();
                        return;
                    }
                    int count = 0;
                    try {
                        count = Integer.parseInt(sec);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    SysConstConfig.getInstance().setErrorCloseSec(count);
                    showState();
                }
            }).show();
        });
        readCardTypeBut.setOnClickListener(view1 -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("请选择读卡方式");
            final Map<String, Integer> map = new LinkedHashMap<>();
            for (ReadCardTypeEmus goo : ReadCardTypeEmus.values()) {
                map.put(goo.getName(), goo.getValue());
            }
            final String[] keysTemp = new String[ReadCardTypeEmus.values().length];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    int state = map.get(keys[which]);
                    if (SysConstConfig.getInstance().getPeripheralDeviceModel() != PeripheralDeviceEmus.defaultType) {
                        if (ReadCardTypeEmus.getByValue(state) == ReadCardTypeEmus.cardSQ) {
                            MsgDialog msgDialog = new MsgDialog(mContext);
                            msgDialog.show();
                            msgDialog.showMsg("外围设备类型不支持该功能，请核对后操作");
                            return;
                        }
                    }
                    SysConstConfig.getInstance().setReadCardType(ReadCardTypeEmus.getByValue(state));
                    showState();
                }
            });
            builder.show();
        });


        cardDataSectorBut.setOnClickListener(view2 -> {
            InputDialog inputDialog = new InputDialog(mContext);
            inputDialog.show();
            inputDialog.getMsgTv().setText("请输入卡资料扇区号");
            inputDialog.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
            inputDialog.getEditText().setText(String.valueOf(SysConstConfig.getInstance().getCardDataSector()));
            inputDialog.getOkTv().setOnClickListener(view1 -> {
                String row = inputDialog.getEditText().getText().toString();
                if (!"".equals(row)) {
                    int rowInt = Integer.parseInt(row);
                    SysConstConfig.getInstance().setCardDataSector(rowInt);
                    inputDialog.dismiss();
                    cardDataSectorBut.setRightText(String.valueOf(SysConstConfig.getInstance().getCardDataSector()));

                } else {
                    Toast.makeText(mContext, "输入有误，请重新输入", Toast.LENGTH_LONG).show();
                }
            });
        });


        cardSectorPassBut.setOnClickListener(view1 -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("请选择卡密码");
            final Map<String, Integer> map = new LinkedHashMap<>();
            for (CardSectorPassEmus goo : CardSectorPassEmus.values()) {
                map.put(goo.getName(), goo.getValue());
            }
            final String[] keysTemp = new String[CardSectorPassEmus.values().length];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    int state = map.get(keys[which]);
                    SysConstConfig.getInstance().setCardSectorPass(CardSectorPassEmus.getByValue(state));
                    showState();
                }
            });
            builder.show();
        });

        offlineGetCardSnTypeBut.setOnClickListener(view1 -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("请选择获取卡序列号方式");
            final Map<String, Integer> map = new LinkedHashMap<>();
            for (OfflineGetCardSnTypeEmus goo : OfflineGetCardSnTypeEmus.values()) {
                map.put(goo.getName(), goo.getValue());
            }
            final String[] keysTemp = new String[OfflineGetCardSnTypeEmus.values().length];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, (dialog, which) -> {
                int state = map.get(keys[which]);
                SysConstConfig.getInstance().setOfflineGetCardSnType(OfflineGetCardSnTypeEmus.getByValue(state));
                showState();
            });
            builder.show();
        });
        showState();
        return view;
    }


    private void showState() {


        if (SysConstConfig.getInstance().isAllowWeixin()) {
            weixinPayBut.setRightText("开启");

        } else {
            weixinPayBut.setRightText("关闭");
        }

        if (SysConstConfig.getInstance().isAllowAli()) {
            aliPayBut.setRightText("开启");

        } else {
            aliPayBut.setRightText("关闭");
        }


        if (SysConstConfig.getInstance().isAllowCard()) {
            cardPayBut.setRightText("开启");

        } else {
            cardPayBut.setRightText("关闭");
        }

        if (SysConstConfig.getInstance().isAllowCash()) {
            cashPayBut.setRightText("开启");
        } else {
            cashPayBut.setRightText("关闭");
        }

        if (SysConstConfig.getInstance().isSuccessClose()) {
            successCloseBut.setRightText("开启");
            successCloseSecBut.setVisibility(View.VISIBLE);
        } else {
            successCloseBut.setRightText("关闭");
            successCloseSecBut.setVisibility(View.GONE);
        }

        if (SysConstConfig.getInstance().isErrorClose()) {
            errorCloseBut.setRightText("开启");
            errorCloseSecBut.setVisibility(View.VISIBLE);
        } else {
            errorCloseBut.setRightText("关闭");
            errorCloseSecBut.setVisibility(View.GONE);
        }

        errorCloseSecBut.setRightText(SysConstConfig.getInstance().getErrorCloseSec() + "秒");
        successCloseSecBut.setRightText(SysConstConfig.getInstance().getSuccessCloseSec() + "秒");
        readCardTypeBut.setRightText(SysConstConfig.getInstance().getReadCardType().getName());
        if(SysConstConfig.getInstance().getReadCardType() == ReadCardTypeEmus.cardSn) {
            offlineGetCardSnTypeBut.setVisibility(View.VISIBLE);
        } else {
            offlineGetCardSnTypeBut.setVisibility(View.GONE);
        }
        offlineGetCardSnTypeBut.setRightText(SysConstConfig.getInstance().getOfflineGetCardSnType().getName());

        if (SysConstConfig.getInstance().getReadCardType() == ReadCardTypeEmus.cardSQ) {
            cardDataSectorBut.setVisibility(View.VISIBLE);
            cardSectorPassBut.setVisibility(View.VISIBLE);
        } else {
            cardDataSectorBut.setVisibility(View.GONE);
            cardSectorPassBut.setVisibility(View.GONE);
        }
        cardDataSectorBut.setRightText(String.valueOf(SysConstConfig.getInstance().getCardDataSector()));
        cardSectorPassBut.setRightText(SysConstConfig.getInstance().getCardSectorPass().getName());
    }
}
